<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">



  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">




  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/avatar.ico?v=6.3.0">


  <link rel="mask-icon" href="/avatar.ico?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.3.0',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="来源和评测点Tyvj1730Bzoj3196Luogu3380Caioj1135">
<meta name="keywords" content="线段树,伸展树,主席树,莫队,难度3,树套树">
<meta property="og:type" content="article">
<meta property="og:title" content="【Bzoj3196】【Luogu3380】二逼平衡树">
<meta property="og:url" content="http://zory.cf/posts/d7e9.html">
<meta property="og:site_name" content="Zory的个人博客">
<meta property="og:description" content="来源和评测点Tyvj1730Bzoj3196Luogu3380Caioj1135">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-09T04:10:20.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【Bzoj3196】【Luogu3380】二逼平衡树">
<meta name="twitter:description" content="来源和评测点Tyvj1730Bzoj3196Luogu3380Caioj1135">






  <link rel="canonical" href="http://zory.cf/posts/d7e9.html"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>【Bzoj3196】【Luogu3380】二逼平衡树 | Zory的个人博客</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?6c38ba8648d29103b8d122f998787d71";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Zory的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">人生即使不能走最短路，仍是连通图</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页Home</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />历程Archives<span class="badge">354</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类Categories<span class="badge">4</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签Tags<span class="badge">133</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-template">
    <a href="/categories/OI之路" rel="section">
      <i class="menu-item-icon fa fa-fw fa-telegram"></i> <br />OI之路</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-problems">
    <a href="/categories/刷题" rel="section">
      <i class="menu-item-icon fa fa-fw fa-list-alt"></i> <br />刷题Problems</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-friends">
    <a href="/friends/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user-circle"></i> <br />友链Friends</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-english">
    <a href="/English/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-etsy"></i> <br />English知识体系</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-boutique">
    <a href="/tags/原创精华/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-handshake-o"></i> <br />原创精华</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-top">
    <a href="/top/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-signal"></i> <br />排行榜Hot</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />找文章强烈建议使用-搜索Search</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="这个搜索超级好用哦！找文章必备！" spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zory.cf/posts/d7e9.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zory">
      <meta itemprop="description" content="人生即使不能走最短路，仍是连通图">
      <meta itemprop="image" content="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zory的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">【Bzoj3196】【Luogu3380】二逼平衡树
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建于：2018-01-23 13:00:00" itemprop="dateCreated datePublished" datetime="2018-01-23T13:00:00+08:00">2018-01-23</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改于：2018-09-09 12:10:20" itemprop="dateModified" datetime="2018-09-09T12:10:20+08:00">2018-09-09</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/刷题/" itemprop="url" rel="index"><span itemprop="name">刷题</span></a></span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/posts/d7e9.html#comments" itemprop="discussionUrl">
                  <span class="post-meta-item-text">评论：</span> <span class="post-comments-count valine-comment-count" data-xid="/posts/d7e9.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/posts/d7e9.html" class="leancloud_visitors" data-flag-title="【Bzoj3196】【Luogu3380】二逼平衡树">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">热度：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="来源和评测点"><a href="#来源和评测点" class="headerlink" title="来源和评测点"></a>来源和评测点</h2><p>Tyvj1730<br><a href="http://www.lydsy.com/JudgeOnline/problem.php?id=3196" target="_blank" rel="noopener">Bzoj3196</a><br><a href="https://www.luogu.org/problemnew/show/P3380" target="_blank" rel="noopener">Luogu3380</a><br><a href="http://caioj.cn/problem.php?id=1135" target="_blank" rel="noopener">Caioj1135</a></p>
<a id="more"></a>
<h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><p id="div-border-top-red"><strong>【题目大意】</strong><br>您需要写一种数据结构（可参考题目标题），来维护一个有序数列，其中需要提供以下操作：<br>1.查询k在区间内的排名<br>2.查询区间内排名为k的值<br>3.修改某一位值上的数值<br>4.查询k在区间内的前驱(前驱定义为小于x，且最大的数)<br>5.查询k在区间内的后继(后继定义为大于x，且最小的数)<br><strong>【输入格式】</strong><br>第一行两个数 n,m 表示长度为n的有序序列和m个操作<br>第二行有n个数，表示有序序列<br>下面有m行，opt表示操作标号<br>若opt=1 则为操作1，之后有三个数l,r,k 表示查询k在区间[l,r]的排名<br>若opt=2 则为操作2，之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数<br>若opt=3 则为操作3，之后有两个数pos,k 表示将pos位置的数修改为k<br>若opt=4 则为操作4，之后有三个数l,r,k 表示查询区间[l,r]内k的前驱<br>若opt=5 则为操作5，之后有三个数l,r,k 表示查询区间[l,r]内k的后继<br><strong>【输出格式】</strong><br>对于操作1,2,4,5各输出一行，表示查询结果<br><strong>【输入样例】</strong><br>9 6<br>4 2 2 1 9 4 0 1 1<br>2 1 4 3<br>3 4 10<br>2 1 4 3<br>1 2 5 9<br>4 3 9 5<br>5 2 8 5<br><strong>【输出样例】</strong><br>2<br>4<br>3<br>4<br>9<br></p>

<h2 id="分析1"><a href="#分析1" class="headerlink" title="分析1"></a>分析1</h2><p><a href="../2003-01/【OI之路】06树-4伸展树.html">其他伸展树</a></p>
<p>第一次树套树，第一次代码行数300+</p>
<p>线段树关键字：位置关系<br>伸展树关键字：大小关系</p>
<p>n=m=50000，num=100000000<br>logn=16，lognum=27<br>建树2n颗，nlogn个节点，时间nlogn</p>
<p>接下来分析几个操作：<br>1.查排名，(区间logn)*(前驱logn)=256，（每个完整子区间中k的排名-1）之和+1<br>2.找数字，(二分lognum)*(区间logn)*(后继logn)=6912，调用操作1，二分数字，验证<br>3.改数字，2*(深度logn)*(伸展操作logn)=512，删除，添加<br>4.5.前驱后继，(区间logn)*(前驱后继logn)=256，每个完整子区间中k的前驱后继的极值<br>总时间：极限200000000即3s</p>
<p>从线段树的角度看：每个区间一个线段树<br>从Splay的角度看：所有的Splay都挂在超级根节点Root上，<br>注意我采取某些手段避免了Root向下认儿子</p>
<h2 id="代码1"><a href="#代码1" class="headerlink" title="代码1"></a>代码1</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="comment">//*******************头文件*******************</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="comment">//*******************全局常量*******************</span></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">50000</span>+<span class="number">10</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> NUL=<span class="number">-1</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> Root=<span class="number">0</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x7fffffff</span>;</div><div class="line"><span class="comment">//*******************Splay1-基础操作*******************</span></div><div class="line"><span class="keyword">struct</span> pt</div><div class="line">&#123;</div><div class="line">	<span class="keyword">int</span> son[<span class="number">2</span>],f;</div><div class="line">	<span class="keyword">int</span> d;</div><div class="line">	<span class="keyword">int</span> n,c;</div><div class="line">	pt()</div><div class="line">	&#123;</div><div class="line">		son[<span class="number">0</span>]=son[<span class="number">1</span>]=f=NUL;</div><div class="line">	&#125;</div><div class="line">&#125;p[<span class="number">30</span>*MAXN];</div><div class="line"><span class="keyword">int</span> ln=<span class="number">0</span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line"></span>&#123;</div><div class="line">	p[x].c=p[x].n;</div><div class="line">	<span class="keyword">int</span> lc=p[x].son[<span class="number">0</span>],rc=p[x].son[<span class="number">1</span>];</div><div class="line">	<span class="keyword">if</span>(lc!=NUL) p[x].c+=p[lc].c;</div><div class="line">	<span class="keyword">if</span>(rc!=NUL) p[x].c+=p[rc].c;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotate</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> w)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> f=p[x].f,ff=p[f].f;</div><div class="line">	</div><div class="line">	<span class="keyword">if</span>(ff!=Root)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span>(p[ff].son[<span class="number">0</span>]==f) p[ff].son[<span class="number">0</span>]=x;</div><div class="line">		<span class="keyword">else</span> p[ff].son[<span class="number">1</span>]=x;</div><div class="line">	&#125;</div><div class="line">	p[x].f=ff;</div><div class="line">	</div><div class="line">	<span class="keyword">int</span> xson=p[x].son[w];</div><div class="line">	<span class="keyword">if</span>(xson!=NUL) p[xson].f=f;</div><div class="line">	p[f].son[<span class="number">1</span>-w]=xson;</div><div class="line">	</div><div class="line">	p[x].son[w]=f;</div><div class="line">	p[f].f=x;</div><div class="line">	</div><div class="line">	update(f);</div><div class="line">	update(x);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> d,<span class="keyword">int</span> f)</span></div><div class="line"></span>&#123;</div><div class="line">	ln++;</div><div class="line">	p[ln].d=d;p[ln].f=f;</div><div class="line">	p[ln].n=p[ln].c=<span class="number">1</span>;</div><div class="line">	p[ln].son[<span class="number">0</span>]=p[ln].son[<span class="number">1</span>]=NUL;</div><div class="line">	<span class="keyword">if</span>(f!=Root)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span>(d&lt;p[f].d) p[f].son[<span class="number">0</span>]=ln;</div><div class="line">		<span class="keyword">else</span> p[f].son[<span class="number">1</span>]=ln;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************Splay2-与根有关*******************</span></div><div class="line"><span class="keyword">int</span> root[<span class="number">2</span>*MAXN];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">splay</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> x,<span class="keyword">int</span> rt)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">while</span>(p[x].f!=rt)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> f=p[x].f,ff=p[f].f;</div><div class="line">		<span class="keyword">if</span>(ff==rt)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">if</span>(p[f].son[<span class="number">0</span>]==x) rotate(x,<span class="number">1</span>);</div><div class="line">			<span class="keyword">else</span> rotate(x,<span class="number">0</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span></div><div class="line">		&#123;</div><div class="line">				 <span class="keyword">if</span>(p[ff].son[<span class="number">0</span>]==f and p[f].son[<span class="number">0</span>]==x) rotate(f,<span class="number">1</span>),rotate(x,<span class="number">1</span>);</div><div class="line">			<span class="keyword">else</span> <span class="keyword">if</span>(p[ff].son[<span class="number">0</span>]==f and p[f].son[<span class="number">1</span>]==x) rotate(x,<span class="number">0</span>),rotate(x,<span class="number">1</span>);</div><div class="line">			<span class="keyword">else</span> <span class="keyword">if</span>(p[ff].son[<span class="number">1</span>]==f and p[f].son[<span class="number">0</span>]==x) rotate(x,<span class="number">1</span>),rotate(x,<span class="number">0</span>);</div><div class="line">			<span class="keyword">else</span> <span class="keyword">if</span>(p[ff].son[<span class="number">1</span>]==f and p[f].son[<span class="number">1</span>]==x) rotate(f,<span class="number">0</span>),rotate(x,<span class="number">0</span>);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span>(rt==Root) root[r]=x;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">findip</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x=root[r];</div><div class="line">	<span class="keyword">while</span>(p[x].d!=d)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> lc=p[x].son[<span class="number">0</span>],rc=p[x].son[<span class="number">1</span>];</div><div class="line">		<span class="keyword">if</span>(d&lt;p[x].d) &#123; <span class="keyword">if</span>(lc!=NUL) x=lc; <span class="keyword">else</span> <span class="keyword">break</span>; &#125;</div><div class="line">		<span class="keyword">else</span>         &#123; <span class="keyword">if</span>(rc!=NUL) x=rc; <span class="keyword">else</span> <span class="keyword">break</span>; &#125;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> x;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ins</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(root[r]==NUL)</div><div class="line">	&#123;</div><div class="line">		add(d,Root);</div><div class="line">		root[r]=ln;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">else</span></div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> x=findip(r,d);</div><div class="line">		<span class="keyword">if</span>(p[x].d==d)</div><div class="line">		&#123;</div><div class="line">			p[x].n++;</div><div class="line">			splay(r,x,Root);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span></div><div class="line">		&#123;</div><div class="line">			add(d,x);</div><div class="line">			splay(r,ln,Root);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">del</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x=findip(r,d);</div><div class="line">	<span class="keyword">if</span>(p[x].d!=d) <span class="keyword">return</span>;</div><div class="line">	splay(r,x,Root);</div><div class="line">	<span class="keyword">if</span>(p[x].n&gt;<span class="number">1</span>)</div><div class="line">	&#123;</div><div class="line">		p[x].n--;</div><div class="line">		update(x);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">else</span></div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> lc=p[x].son[<span class="number">0</span>],rc=p[x].son[<span class="number">1</span>];</div><div class="line">		<span class="keyword">if</span>(lc==NUL and rc==NUL)</div><div class="line">		&#123;</div><div class="line">			root[r]=NUL;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(lc!=NUL and rc==NUL)</div><div class="line">		&#123;</div><div class="line">			root[r]=lc;</div><div class="line">			p[lc].f=Root;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(lc==NUL and rc!=NUL)</div><div class="line">		&#123;</div><div class="line">			root[r]=rc;</div><div class="line">			p[rc].f=Root;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span></div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> w=lc;<span class="keyword">while</span>(p[w].son[<span class="number">1</span>]&gt;<span class="number">0</span>) w=p[w].son[<span class="number">1</span>];</div><div class="line">			</div><div class="line">			splay(r,w,x);root[r]=w;p[w].f=Root;</div><div class="line">			p[w].son[<span class="number">1</span>]=rc;p[rc].f=w;</div><div class="line">			</div><div class="line">			update(w);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">findQ</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x=findip(r,d);splay(r,x,Root);</div><div class="line">	<span class="keyword">if</span>(p[x].d&gt;=d and p[x].son[<span class="number">0</span>]!=NUL)</div><div class="line">	&#123;</div><div class="line">		x=p[x].son[<span class="number">0</span>];</div><div class="line">		<span class="keyword">while</span>(p[x].son[<span class="number">1</span>]!=NUL) x=p[x].son[<span class="number">1</span>];</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span>(p[x].d&lt;d) <span class="keyword">return</span> p[x].d;</div><div class="line">	<span class="keyword">return</span> -INF;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">findH</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x=findip(r,d);splay(r,x,Root);</div><div class="line">	<span class="keyword">if</span>(p[x].d&lt;=d and p[x].son[<span class="number">1</span>]!=NUL)</div><div class="line">	&#123;</div><div class="line">		x=p[x].son[<span class="number">1</span>];</div><div class="line">		<span class="keyword">while</span>(p[x].son[<span class="number">0</span>]!=NUL) x=p[x].son[<span class="number">0</span>];</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span>(p[x].d&gt;d) <span class="keyword">return</span> p[x].d;</div><div class="line">	<span class="keyword">return</span> INF;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">findbf</span><span class="params">(<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span><span class="comment">//更小的</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x=findip(r,d);splay(r,x,Root);</div><div class="line">	<span class="keyword">int</span> lc=p[x].son[<span class="number">0</span>];</div><div class="line">	<span class="keyword">if</span>(p[x].d==d) <span class="keyword">return</span> p[ (lc==NUL)?<span class="number">0</span>:lc ].c;</div><div class="line">	</div><div class="line">	<span class="keyword">if</span>(p[x].d&gt;d and lc!=NUL)</div><div class="line">	&#123;</div><div class="line">		x=lc;</div><div class="line">		<span class="keyword">while</span>(p[x].son[<span class="number">1</span>]!=NUL) x=p[x].son[<span class="number">1</span>];</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span>(p[x].d&lt;d)</div><div class="line">	&#123;</div><div class="line">		splay(r,x,Root);lc=p[x].son[<span class="number">0</span>];</div><div class="line">		<span class="keyword">return</span> p[ (lc==NUL)?<span class="number">0</span>:lc ].c+p[x].n;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************线段树*******************</span></div><div class="line"><span class="keyword">int</span> num[MAXN];</div><div class="line"><span class="keyword">struct</span> mg</div><div class="line">&#123;</div><div class="line">	<span class="keyword">int</span> l,r;</div><div class="line">	<span class="keyword">int</span> lc,rc;</div><div class="line">&#125;s[<span class="number">2</span>*MAXN];</div><div class="line"><span class="keyword">int</span> len=<span class="number">0</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">build</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> t=++len;s[t].l=l;s[t].r=r;</div><div class="line">	</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=l;i&lt;=r;i++) ins(t,num[i]);</div><div class="line">	<span class="comment">//把t作为根节点的下标</span></div><div class="line">	</div><div class="line">	<span class="keyword">if</span>(l==r) s[t].lc=s[t].rc=NUL;</div><div class="line">	<span class="keyword">else</span></div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> mid=(l+r)/<span class="number">2</span>;</div><div class="line">		s[t].lc=build(l,mid);</div><div class="line">		s[t].rc=build(mid+<span class="number">1</span>,r);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> t;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">change</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> pos,<span class="keyword">int</span> yd,<span class="keyword">int</span> d)</span><span class="comment">//改数字</span></div><div class="line"></span>&#123;</div><div class="line">	del(x,yd);ins(x,d);</div><div class="line">	<span class="keyword">if</span>(s[x].l==s[x].r) <span class="keyword">return</span>;</div><div class="line">	<span class="keyword">int</span> mid=(s[x].l+s[x].r)/<span class="number">2</span>;</div><div class="line">	<span class="keyword">if</span>( pos&lt;=mid ) change(s[x].lc,pos,yd,d);</div><div class="line">	<span class="keyword">else</span> change(s[x].rc,pos,yd,d);</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************接口*******************</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve1</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span><span class="comment">//查排名</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(s[x].l==l and s[x].r==r) <span class="keyword">return</span> findbf(x,d);</div><div class="line">	<span class="keyword">int</span> lc=s[x].lc,rc=s[x].rc,mid=(s[x].l+s[x].r)/<span class="number">2</span>;</div><div class="line">	<span class="keyword">if</span>(r&lt;=mid) <span class="keyword">return</span> solve1(lc,l,r,d);</div><div class="line">	<span class="keyword">if</span>(l&gt;mid)  <span class="keyword">return</span> solve1(rc,l,r,d);</div><div class="line">	<span class="keyword">return</span> solve1(lc,l,mid,d)+solve1(rc,mid+<span class="number">1</span>,r,d);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve2</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span><span class="comment">//找数字</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> ll=<span class="number">0</span>,rr=<span class="number">100000000</span>,ans=<span class="number">-1</span>;</div><div class="line">	<span class="comment">//满足运用二分的条件，因为数字与排名成正比</span></div><div class="line">	<span class="keyword">while</span>(ll&lt;=rr)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> mid=(ll+rr)/<span class="number">2</span>,rk=solve1(<span class="number">1</span>,l,r,mid)+<span class="number">1</span>;</div><div class="line">		<span class="keyword">if</span>(rk&lt;=k) ans=mid,ll=mid+<span class="number">1</span>;</div><div class="line">		<span class="keyword">else</span> rr=mid<span class="number">-1</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> ans;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve4</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span><span class="comment">//前驱</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(s[x].l==l and s[x].r==r)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> t=findQ(x,d);</div><div class="line">		<span class="keyword">return</span> (t==NUL)?<span class="number">0</span>:t;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">int</span> lc=s[x].lc,rc=s[x].rc,mid=mid=(s[x].l+s[x].r)/<span class="number">2</span>;</div><div class="line">	<span class="keyword">if</span>(r&lt;=mid) <span class="keyword">return</span> solve4(lc,l,r,d);</div><div class="line">	<span class="keyword">if</span>(l&gt;mid)  <span class="keyword">return</span> solve4(rc,l,r,d);</div><div class="line">	<span class="keyword">return</span> mymax( solve4(lc,l,mid,d),solve4(rc,mid+<span class="number">1</span>,r,d) );</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve5</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> d)</span><span class="comment">//后继</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(s[x].l==l and s[x].r==r)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> t=findH(x,d);</div><div class="line">		<span class="keyword">return</span> (t==NUL)?<span class="number">0x3f3f3f3f</span>:t;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">int</span> lc=s[x].lc,rc=s[x].rc,mid=(s[x].l+s[x].r)/<span class="number">2</span>;</div><div class="line">	<span class="keyword">if</span>(r&lt;=mid) <span class="keyword">return</span> solve5(lc,l,r,d);</div><div class="line">	<span class="keyword">if</span>(l&gt;mid)  <span class="keyword">return</span> solve5(rc,l,r,d);</div><div class="line">	<span class="keyword">return</span> mymin( solve5(lc,l,mid,d),solve5(rc,mid+<span class="number">1</span>,r,d) );</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************主函数*******************</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="built_in">memset</span>(root,NUL,<span class="keyword">sizeof</span>(root));</div><div class="line">	</div><div class="line">	<span class="keyword">int</span> n,m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;num[i]);</div><div class="line">	build(<span class="number">1</span>,n);</div><div class="line">	<span class="keyword">while</span>(m--)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> op;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;op);</div><div class="line">		<span class="keyword">if</span>(op==<span class="number">3</span>)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> pos,d;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;pos,&amp;d);</div><div class="line">			change(<span class="number">1</span>,pos,num[pos],d);num[pos]=d;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">else</span></div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> l,r,k;<span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&amp;l,&amp;r,&amp;k);</div><div class="line">			<span class="keyword">if</span>(op==<span class="number">1</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve1(<span class="number">1</span>,l,r,k)+<span class="number">1</span>);</div><div class="line">			<span class="keyword">if</span>(op==<span class="number">2</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve2(l,r,k));</div><div class="line">			<span class="keyword">if</span>(op==<span class="number">4</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve4(<span class="number">1</span>,l,r,k));</div><div class="line">			<span class="keyword">if</span>(op==<span class="number">5</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve5(<span class="number">1</span>,l,r,k));</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="分析2"><a href="#分析2" class="headerlink" title="分析2"></a>分析2</h2><p>然鹅Bzoj的评论里面有这样几句话：<br>“用平衡树的都是二逼青年”<br>“有智慧的长者都写主席树”<br>so，依旧被D飞<br>赶紧复习一波主席树</p>
<p>哦补充一下，我在打的时候突然想到一个问题：<br>假如r的lowbit覆盖了l-1,那么它不就会turn两次了？<br>后来经提醒发现，这种turn两次的情况理论上虽然是错的，<br>但因为他们都是被覆盖两次的，所以求前缀和的时候刚好抵消了……</p>
<p>优点就是少了一百行……，速度还快了三倍！</p>
<h2 id="代码2"><a href="#代码2" class="headerlink" title="代码2"></a>代码2</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="comment">//*******************主函数******************</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> WIN32</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> BIGN <span class="meta-string">"%I64d"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">else</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> BIGN <span class="meta-string">"%lld"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="comment">//*******************全局常量*******************</span></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">50010</span>,MAXM=<span class="number">50010</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x7fffffff</span>;</div><div class="line"><span class="comment">//*******************全局定义*******************</span></div><div class="line"><span class="keyword">int</span> n;</div><div class="line"><span class="keyword">int</span> rx;</div><div class="line"><span class="keyword">int</span> num[MAXN+MAXM];</div><div class="line"><span class="keyword">int</span> now[MAXN];</div><div class="line"><span class="comment">//*******************主席树******************</span></div><div class="line"><span class="keyword">struct</span> Nod</div><div class="line">&#123;</div><div class="line">	<span class="keyword">int</span> c;</div><div class="line">	<span class="keyword">int</span> lc,rc;</div><div class="line">	Nod() &#123;c=lc=rc=<span class="number">0</span>;&#125;</div><div class="line">&#125;p[MAXN*<span class="number">20</span>*<span class="number">10</span>];</div><div class="line"><span class="keyword">int</span> rt[MAXN];</div><div class="line"><span class="keyword">int</span> ust[MAXN];</div><div class="line"><span class="keyword">int</span> cnt=<span class="number">0</span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> &amp;x,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> pos,<span class="keyword">int</span> c)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(!x) x=++cnt;</div><div class="line">	p[x].c+=c;</div><div class="line">	<span class="keyword">if</span>(l==r) <span class="keyword">return</span>;</div><div class="line">	<span class="keyword">int</span> mid=(l+r)&gt;&gt;<span class="number">1</span>;</div><div class="line">	<span class="keyword">if</span>(pos&lt;=mid) add(p[x].lc,l,mid,pos,c);</div><div class="line">	<span class="keyword">else</span> add(p[x].rc,mid+<span class="number">1</span>,r,pos,c);</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************树状数组******************</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">lowbit</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&amp;(-x);&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">change</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> pos,<span class="keyword">int</span> c)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">while</span>(x&lt;=n)</div><div class="line">	&#123;</div><div class="line">		add(rt[x],<span class="number">1</span>,rx,pos,c);</div><div class="line">		x+=lowbit(x);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">getlsum</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> s=<span class="number">0</span>;</div><div class="line">	<span class="keyword">while</span>(x&gt;=<span class="number">1</span>)</div><div class="line">	&#123;</div><div class="line">		s+=p[ p[ust[x]].lc ].c;</div><div class="line">		x-=lowbit(x);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> s;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">getsum</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> s=<span class="number">0</span>;</div><div class="line">	<span class="keyword">while</span>(x&gt;=<span class="number">1</span>)</div><div class="line">	&#123;</div><div class="line">		s+=p[ust[x]].c;</div><div class="line">		x-=lowbit(x);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> s;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">getrsum</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> s=<span class="number">0</span>;</div><div class="line">	<span class="keyword">while</span>(x&gt;=<span class="number">1</span>)</div><div class="line">	&#123;</div><div class="line">		s+=p[ p[ust[x]].rc ].c;</div><div class="line">		x-=lowbit(x);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> s;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">turn</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> tn)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">while</span>(x&gt;=<span class="number">1</span>)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span>(tn==<span class="number">0</span>) ust[x]=rt[x];</div><div class="line">		<span class="keyword">if</span>(tn==<span class="number">-1</span>) ust[x]=p[ ust[x] ].lc;</div><div class="line">		<span class="keyword">if</span>(tn==<span class="number">1</span>) ust[x]=p[ ust[x] ].rc;</div><div class="line">		x-=lowbit(x);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************实现******************</span></div><div class="line"><span class="keyword">struct</span> Qes</div><div class="line">&#123;</div><div class="line">	<span class="keyword">int</span> op;</div><div class="line">	<span class="keyword">int</span> x,y,k;</div><div class="line">&#125;q[MAXM];</div><div class="line"><span class="keyword">int</span> qian;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ask1</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(l==r) <span class="keyword">return</span> qian;</div><div class="line">	<span class="keyword">int</span> mid=(l+r)&gt;&gt;<span class="number">1</span>;</div><div class="line">	<span class="keyword">if</span>(k&lt;=mid)</div><div class="line">	&#123;</div><div class="line">		turn(x<span class="number">-1</span>,<span class="number">-1</span>);turn(y,<span class="number">-1</span>);</div><div class="line">		<span class="keyword">return</span> ask1(x,y,l,mid,k);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">else</span></div><div class="line">	&#123;</div><div class="line">		qian+=getlsum(y)-getlsum(x<span class="number">-1</span>);</div><div class="line">		turn(x<span class="number">-1</span>,<span class="number">1</span>);turn(y,<span class="number">1</span>);</div><div class="line">		<span class="keyword">return</span> ask1(x,y,mid+<span class="number">1</span>,r,k);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve1</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span><span class="comment">//k在区间内的排名</span></div><div class="line"></span>&#123;</div><div class="line">	turn(l<span class="number">-1</span>,<span class="number">0</span>);turn(r,<span class="number">0</span>);</div><div class="line">	qian=<span class="number">0</span>;</div><div class="line">	<span class="keyword">return</span> ask1(l,r,<span class="number">1</span>,rx,k)+<span class="number">1</span>;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ask2</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(l==r) <span class="keyword">return</span> num[l];</div><div class="line">	<span class="keyword">int</span> ls=getlsum(y)-getlsum(x<span class="number">-1</span>);</div><div class="line">	<span class="keyword">int</span> mid=(l+r)&gt;&gt;<span class="number">1</span>;</div><div class="line">	<span class="keyword">if</span>(k&lt;=ls)</div><div class="line">	&#123;</div><div class="line">		turn(x<span class="number">-1</span>,<span class="number">-1</span>);turn(y,<span class="number">-1</span>);</div><div class="line">		<span class="keyword">return</span> ask2(x,y,l,mid,k);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">else</span></div><div class="line">	&#123;</div><div class="line">		turn(x<span class="number">-1</span>,<span class="number">1</span>);turn(y,<span class="number">1</span>);</div><div class="line">		<span class="keyword">return</span> ask2(x,y,mid+<span class="number">1</span>,r,k-ls);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve2</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span><span class="comment">//区间内排名为k的值</span></div><div class="line"></span>&#123;</div><div class="line">	turn(l<span class="number">-1</span>,<span class="number">0</span>);turn(r,<span class="number">0</span>);</div><div class="line">	<span class="keyword">return</span> ask2(l,r,<span class="number">1</span>,rx,k);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve3</span><span class="params">(<span class="keyword">int</span> pos,<span class="keyword">int</span> k)</span><span class="comment">//修改</span></div><div class="line"></span>&#123;</div><div class="line">	change(pos,now[pos],<span class="number">-1</span>);</div><div class="line">	now[pos]=k;</div><div class="line">	change(pos,now[pos],<span class="number">1</span>);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve4</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span><span class="comment">//查询k在区间内的前驱，没有则-INF</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> t=solve1(l,r,k)<span class="number">-1</span>;</div><div class="line">	<span class="keyword">if</span>(t==<span class="number">0</span>) <span class="keyword">return</span> -INF;</div><div class="line">	<span class="keyword">return</span> solve2(l,r,t);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">solve5</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> k)</span><span class="comment">//查询k在区间内的后继，没有则INF</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> t=solve1(l,r,k+<span class="number">1</span>)<span class="number">-1</span>;</div><div class="line">	<span class="keyword">if</span>(t==(r-l+<span class="number">1</span>)) <span class="keyword">return</span> INF;</div><div class="line">	<span class="keyword">return</span> solve2(l,r,t+<span class="number">1</span>);</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************离散化******************</span></div><div class="line"><span class="keyword">struct</span> Lsh</div><div class="line">&#123;</div><div class="line">	<span class="keyword">int</span> x,id,z;</div><div class="line">&#125;a[MAXN+MAXM];</div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(Lsh a,Lsh b)</span> </span>&#123;<span class="keyword">return</span> a.x&lt;b.x;&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">lsh</span><span class="params">(<span class="keyword">int</span> all)</span></div><div class="line"></span>&#123;</div><div class="line">	sort(a+<span class="number">1</span>,a+all+<span class="number">1</span>,cmp);</div><div class="line">	rx=<span class="number">0</span>;a[<span class="number">0</span>].x=-INF;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=all;i++)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span>(a[i<span class="number">-1</span>].x!=a[i].x) num[++rx]=a[i].x;<span class="comment">//debug</span></div><div class="line">		</div><div class="line">		<span class="keyword">if</span>(a[i].id&lt;=n) a[a[i].id].z=rx;</div><div class="line">		<span class="keyword">else</span> q[a[i].id-n].k=rx;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">//*******************主函数******************</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i].x),a[i].id=i;</div><div class="line">	<span class="keyword">int</span> all=n;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)</div><div class="line">	&#123;</div><div class="line">		<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;q[i].op,&amp;q[i].x);</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">2</span>) <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;q[i].y,&amp;q[i].k);</div><div class="line">		<span class="keyword">else</span></div><div class="line">		&#123;</div><div class="line">			all++;</div><div class="line">			<span class="keyword">if</span>(q[i].op!=<span class="number">3</span>) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;q[i].y);</div><div class="line">			<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[all].x);</div><div class="line">			a[all].id=n+i;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	lsh(all);</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) now[i]=a[i].z,change(i,now[i],<span class="number">1</span>);</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">1</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve1(q[i].x,q[i].y,q[i].k));</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">2</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve2(q[i].x,q[i].y,q[i].k));</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">3</span>) solve3(q[i].x,q[i].k);</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">4</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve4(q[i].x,q[i].y,q[i].k));</div><div class="line">		<span class="keyword">if</span>(q[i].op==<span class="number">5</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,solve5(q[i].x,q[i].y,q[i].k));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>

    
    
    

    
      <div class="post-tags">
        
          <a href="/tags/线段树/" rel="tag"># 线段树</a>
        
          <a href="/tags/伸展树/" rel="tag"># 伸展树</a>
        
          <a href="/tags/主席树/" rel="tag"># 主席树</a>
        
          <a href="/tags/莫队/" rel="tag"># 莫队</a>
        
          <a href="/tags/难度3/" rel="tag"># 难度3</a>
        
          <a href="/tags/树套树/" rel="tag"># 树套树</a>
        
      </div>
    

    
      

  <div class="popular-posts-header">相关文章推荐</div>
  <ul class="popular-posts">
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/b112.html" rel="bookmark">花园</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/edb5.html" rel="bookmark">【Bzoj1835】【Luogu2605】基站选址</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/8705.html" rel="bookmark">【Bzoj1901】Dynamic Rankings</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/506a.html" rel="bookmark">【OI之路】06树-3伸展树</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/739e.html" rel="bookmark">糖果自动管理系统</a></div>
      
    </li>
  
  </ul>


    

	
		<div>
				<p id="div-border-left-purple">
			   <b>本文基于<a target="_blank" title="Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)" href="http://creativecommons.org/licenses/by-sa/4.0/deed.zh"> 知识共享署名-相同方式共享 4.0 </a>国际许可协议发布</b><br/>
				<span>
				<b>本文地址：</b><a href="/posts/d7e9.html" title="【Bzoj3196】【Luogu3380】二逼平衡树">http://zory.cf/posts/d7e9.html</a><br/><b>转载请注明出处，谢谢！</b>
				</span>
				</p>
		</div>
    

    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>哪怕是一杯奶茶，也将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="Zory 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/f8f3.html" rel="next" title="WZK的减肥计划">
                <i class="fa fa-chevron-left"></i> WZK的减肥计划
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/3834.html" rel="prev" title="【置顶】码风与卡常技巧">
                【置顶】码风与卡常技巧 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg"
                alt="Zory" />
            
              <p class="site-author-name" itemprop="name">Zory</p>
              <p class="site-description motion-element" itemprop="description">人生即使不能走最短路，仍是连通图</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">354</span>
                    <span class="site-state-item-name">文章</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">133</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/Zory123" target="_blank" title="GitHub" rel="external nofollow"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:1134430323@qq.com" target="_blank" title="E-Mail" rel="external nofollow"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

<script type="text/javascript" src="https://api.lwl12.com/hitokoto/main/get?encode=js&charset=utf-8"></script>
<div id="lwlhitokoto">
    <script>
        lwlhitokoto()
    </script>
</div>

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#来源和评测点"><span class="nav-text">来源和评测点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#题目"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分析1"><span class="nav-text">分析1</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码1"><span class="nav-text">代码1</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分析2"><span class="nav-text">分析2</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码2"><span class="nav-text">代码2</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zory</span>

  

  
</div>


  









  <div class="footer-custom">Hosted by <a target="_blank" rel="external nofollow" href="https://pages.coding.me"><b>Coding Pages</b></a></div>


        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  















  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



	





  





  








  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(function (item) {
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: true,
        appId: 'zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz',
        appKey: 'cJDLxDoNgzq0JJUWQFmpCRdw',
        placeholder: '对这篇文章有什么看法？大胆地说出来吧',
        avatar:'wavatar',
        guest_info:guest,
        pageSize:'20' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('2');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz", "cJDLxDoNgzq0JJUWQFmpCRdw");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
            counter.save(null, {
              success: function(counter) {
                
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(counter.get('time'));
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var newcounter = new Counter();
              /* Set ACL */
              var acl = new AV.ACL();
              acl.setPublicReadAccess(true);
              acl.setPublicWriteAccess(true);
              newcounter.setACL(acl);
              /* End Set ACL */
              newcounter.set("title", title);
              newcounter.set("url", url);
              newcounter.set("time", 1);
              newcounter.save(null, {
                success: function(newcounter) {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
                },
                error: function(newcounter, error) {
                  console.log('Failed to create');
                }
              });
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  

  
    
      <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      },
      TeX: {equationNumbers: { autoNumber: "AMS" }}
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/mathjax@2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

    
  


  
  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook,GooglePlus,Evernote,Vkontakte";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  
  
  
  <script src="/lib/pangu/dist/pangu.min.js?v=3.3"></script>
  <script type="text/javascript">pangu.spacingPage();</script>


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=6.3.0"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=6.3.0"></script>


  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('Copy').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('Copied')
          else $(this).text('Copy failed')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('Copy')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
